package com.findtech.threePomelos.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVMobilePhoneVerifyCallback;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.findtech.threePomelos.base.MyActionBarActivity;
import com.findtech.threePomelos.base.MyApplication;
import com.findtech.threePomelos.R;
import com.findtech.threePomelos.utils.RequestUtils;
import com.findtech.threePomelos.utils.ToastUtil;

public class PhoneVerifyActivity extends MyActionBarActivity implements View.OnClickListener {

    private Button mIdentify;
    private Button nextButton;

    private EditText phoneTextView;
    private EditText phoneIdentifyTextView;

    private String correctPhoneNum;
    boolean canSendRequst = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_phone_verify);
        setToolbar(getResources().getString(R.string.phone_verify), true);

        phoneTextView = (EditText) findViewById(R.id.input_num);
        phoneIdentifyTextView = (EditText) findViewById(R.id.identify_num);
        //passwordTextView.setTypeface(Typeface.SANS_SERIF);

        mIdentify = (Button) findViewById(R.id.send_identify);
        nextButton = (Button) findViewById(R.id.next);

        mIdentify.setOnClickListener(this);
        nextButton.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        phoneTextView.setText("");
        phoneIdentifyTextView.setText("");
    }

    @Override
    public void onClick(View view) {
        String phoneStr = phoneTextView.getText().toString();
        switch (view.getId()) {
            case R.id.send_identify:
                if (checkLegalUser(phoneStr)) {
                    correctPhoneNum = phoneStr;
                    AVOSCloud.requestSMSCodeInBackground(phoneStr, "三个柚子", null, 2, new RequestMobileCodeCallback() {
                        @Override
                        public void done(AVException e) {
                            Log.d("ZZ", "canSendRequst e = " + e);
                            if (e == null) {
                                Toast.makeText(getApplicationContext(), "验证码已经发送,有效期2分钟哦", Toast.LENGTH_SHORT).show();
                            } else {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                break;
            case R.id.next:
                if (MyApplication.DEBUG) {
                    RequestUtils.getSharepreferenceEditor(this)
                            .putString(RequestUtils.USERNAME, phoneTextView.getText().toString()).commit();
                    Intent intent = new Intent(PhoneVerifyActivity.this, SetPasswordActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//                    intent.putExtra(RequestTools.NetDatabase.USERNAME, correctPhoneNum);
                    startActivity(intent);
                } else {
                    String identifyCode = phoneIdentifyTextView.getText().toString();
                    if (!checkLegalUser(phoneStr)) {
                        return;
                    }
                    if (TextUtils.isEmpty(identifyCode)) {
                        ToastUtil.showToast(this, R.string.hint_identify_text);
                        return;
                    }
                    if (!TextUtils.isEmpty(correctPhoneNum)) {
                        verifySMSCode(identifyCode, correctPhoneNum);
                    }
                }
                break;
        }
    }

    private void verifySMSCode(String code, String phone) {

        AVOSCloud.verifySMSCodeInBackground(code, phone, new AVMobilePhoneVerifyCallback() {
            @Override
            public void done(AVException e) {
                if (e == null) {
                    RequestUtils.getSharepreferenceEditor(PhoneVerifyActivity.this)
                            .putString(RequestUtils.USERNAME, correctPhoneNum).commit();
                    Intent intent = new Intent(PhoneVerifyActivity.this, SetPasswordActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                } else {
                    if (e.getCode() == 603) {
                        ToastUtil.showToast(PhoneVerifyActivity.this, "无效的短信验证码，通常是不匹配或者过期。");
                    }
                    e.printStackTrace();
                }
            }
        });
    }

    private boolean checkLegalUser(String phoneStr) {
        boolean canSendRequest = true;
        if (TextUtils.isEmpty(phoneStr)) {
            canSendRequest = false;
            ToastUtil.showToast(this, R.string.input_phone_number);
        } else if (!phoneStr.isEmpty() && !phoneStr.matches("^(13|14|15|17|18)\\d{9}$")) {
            canSendRequest = false;
            ToastUtil.showToast(this, R.string.input_legal_phone_number);
        }
        return canSendRequest;
    }
}
